package com.qf.leadnewsarticle.controller.v1;

import com.qf.leadnewsarticle.model.dtos.BehaviorDto;
import com.qf.leadnewsarticle.service.RedisService;
import com.qf.leadnewsmodel.consts.BehaviorConst;
import com.qf.leadnewsmodel.dtos.ResponseResult;
import com.qf.leadnewsmodel.enums.AppHttpCodeEnum;
import com.qf.leadnewsutils.commons.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/v1")
public class ArticleBehaviorController {

    @Autowired
    private RedisService redisService;

    @RequestMapping("/collection_behavior")
    public ResponseResult collection(@RequestBody BehaviorDto behaviorDto){

        String key = BehaviorConst.ARTICLE_COLLECTION_PREFIX + behaviorDto.getEntryId();
        Long offset = ThreadLocalUtil.getUid();
        Integer ops = behaviorDto.getOperation();

        redisService.bitmap(key,offset,ops);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @RequestMapping("/article/load_article_behavior")
    public ResponseResult loadBehavior(@RequestBody BehaviorDto behaviorDto){

        Long articleId = behaviorDto.getArticleId();
        Long uid = ThreadLocalUtil.getUid();

        boolean isLike = redisService.isTrue(BehaviorConst.ARTICLE_LIKES_PREFIX + articleId, uid);
        boolean isUnLike = redisService.isTrue(BehaviorConst.ARTICLE_UN_LIKES_PREFIX + articleId, uid);
        boolean isCollection = redisService.isTrue(BehaviorConst.ARTICLE_COLLECTION_PREFIX + articleId, uid);

        Map map = new HashMap();
        map.put("islike",isLike);
        map.put("isunlike",isUnLike);
        map.put("iscollection",isCollection);

        return ResponseResult.okResult(map);
    }

}
